/**
 * 基础陀螺类
 * 1.完成陀螺的四种状态切换
 * 2.完成陀螺碰撞的逻辑
 * 
 */


class basicTop extends BasicObj {
	public constructor() {
		super();
		this.Init();
	}
	/**VIEW---------------------------------- */
	/**陀螺内（圆） */
	protected top_Inner: eui.Image;
	/**陀螺外（刀刃） */
	protected top_outer: eui.Image;
	/**半径 */
	protected radius: number;
	/**陀螺的碰撞范围与陀螺大小的比例 */
	protected ratio: number;

	/**血量 */
	protected blood: number;
	/**陀螺大小所有的档位 */
	protected radiusGears = [200, 180, 140, 100, 60];

	protected myRock;
	protected myLastStates;
	//防止撞击到同样的人
	protected LastCrashObj;

	/**动画显示对象 */
	protected armatureDisplay: dragonBones.EgretArmatureDisplay;

	/**显示名字的文本 */
	protected nameText: eui.Label;

	/**临时接收显示血量对象 */
	protected showBloodText: eui.Label;

	/**記錄下原來的尺寸 */
	protected nowBloodSize: Number;

	/*记录一下PK的对象 */

	protected PKObj: basicTop;
	/**能量 */
	protected energy: number;

	//当发生碰撞的时候.整个逻辑进行更改
	protected myStates =
	{
		state: 0,
		lastState: 0,
		statesStartTime: 0,

	};
	/**能量 */
	protected pkValue = 100;
	//初始化
	private Init() {
		this.data_init();
		this.uiInit();
	}
	/**数据初始化 */
	private data_init() {
		this.energy = 0;
		this.blood = 100;
		this.ratio = this.myCrashRange / this.radius;
		this.rotateSpeed = 4;
	}



	/**设置PK的对象 */
	private setPKObj(tbasicTop: basicTop) {
		if (tbasicTop == null) {
			//	console.log("陀螺对象显示为空");
			return;
		}
		if (this.PKObj != null) {
			//	console.log("陀螺对象重复使用");
			return;
		}
		this.PKObj = tbasicTop;
	}

	//ui的初始化
	private uiInit() {

		this.addTopName();

		this.showBlood();
	}





	protected tmpPoint = new egret.Point();

	/**根據我們的需求，將轉速變成速度 */
	public changeRotataonSpeedToSpeed() {
		this.speed = this.rotateSpeed * 2;
	}



	/**计算移动 */
	public caculataMove() {

		if (this.myStates.state == TopState.normal) {
			this.changeRotataonSpeedToSpeed();

			this.downRotateSpeed();
		}

		this.dataTargetDir.normalize(this.speed);
		//w
		this.tmpPoint.setTo((this.dataPoint.x + this.dataTargetDir.x), (this.dataPoint.y + this.dataTargetDir.y));


		if (this.dataPoint == null) {
			console.log("移动一个不存在的陀螺");
			return;
		}
		//
		if (this.checkBound(this.tmpPoint, this) == false) {
			this.dataPoint.x += this.dataTargetDir.x;
			this.dataPoint.y += this.dataTargetDir.y;
		}
		else {
			if (this.dataPoint.x < (serverConfig.config.boundSize.x1 + this.myCrashRange)) {
				this.dataPoint.x = serverConfig.config.boundSize.x1 + this.myCrashRange;
			}
			else if (this.dataPoint.x > (serverConfig.config.boundSize.x2 - this.myCrashRange)) {
				this.dataPoint.x = serverConfig.config.boundSize.x2 - this.myCrashRange;
			}

			if (this.dataPoint.y < (serverConfig.config.boundSize.y1 + this.myCrashRange)) {
				this.dataPoint.y = serverConfig.config.boundSize.y1 + this.myCrashRange;
			}

			else if (this.dataPoint.y > (serverConfig.config.boundSize.y2 - this.myCrashRange)) {
				this.dataPoint.y = serverConfig.config.boundSize.y2 - this.myCrashRange;
			}

		}
	}
	public dataUpdate() {

		// if (this.states != TopState.normal)
		// 	return;

		if (this.isAI == true) {
			return;
		}


		if (this.myStates.state != TopState.bePk) {
			this.caculataMove();
		}




		this.updateMyState()

	}
	/**加转速 */
	public addRotateSpeed() {
		// this.rotateSpeed += 0.1;
	}
	public downRotateSpeed() {
		this.rotateSpeed -= serverConfig.config.speedSize.SpeedDown;

		if (this.rotateSpeed <= serverConfig.config.speedSize.minSpeedRotateSpeed)
			this.rotateSpeed = serverConfig.config.speedSize.minSpeedRotateSpeed;
	}

	//加载名字
	public addTopName() {
		this.nameText = new eui.Label();
		this.nameText.text = this.nickName;
		this.nameText.anchorOffsetX = this.nameText.width / 2;
		this.nameText.anchorOffsetY = this.nameText.height / 2;
		this.nameText.x = 0;
		this.nameText.y = -70;
		this.addChild(this.nameText);
	}

	private lastfaceStates;
	//根据速度来切换脸部表情
	public setSpeedFace(facestate) {

		if (facestate == this.lastfaceStates) {
			return;
		}
		this.lastfaceStates = facestate;

		this.changeExpression(facestate);

	}

	public getSpeedLevel() {
		return this.speedLevel;
	}
	public speedLevel = 0;
	public updateFace() {
		let num = serverConfig.config.speedRange.max;
		let verSpeed = serverConfig.config.speedSize.maxSpeedRotateSpeed / num;
		if (this.rotateSpeed < verSpeed) {
			this.speedLevel = ExpressionState.normal;
			this.setSpeedFace(ExpressionState.normal);
		}
		else if (this.rotateSpeed < verSpeed * 2) {
			this.speedLevel = ExpressionState.abnormal;
			this.setSpeedFace(ExpressionState.abnormal);
		}
		else if (this.rotateSpeed < verSpeed * 3) {
			this.speedLevel = ExpressionState.insane;
			this.setSpeedFace(ExpressionState.insane);
		}
		else {
			console.log("换取了错误的速度");
		}
	}

	//更改整个速度
	public changeSpeed(vstates, kp) {
		let steatsTmpTime = this.speed;
		switch (vstates) {
			case TopState.normal:

				break;

			case TopState.death:
				steatsTmpTime = 0
				break;
			case TopState.shoot:
				steatsTmpTime = 50
				break;

			case TopState.beCrash:
				steatsTmpTime = 30
				break;

			case TopState.bePk:
				steatsTmpTime = 0
				break;

			default:
				console.log("getStatesTime 出現問題");
				break;
		}
		return steatsTmpTime * kp;
	}

	public changeStates(states) {
		/**如果是碰撞狀態，則有可能出現被其他物體二次碰撞 */



		if (states != this.myLastStates) {

			this.changeMyState(states);

		}



	}

	/**获取玩家的摇杆
	 * 请用该函数获取摇杆，不要直接用变量，防止重复调用函数
	 * 
	 */
	protected getGameRock() {


		if (this.myRock == null) {


			this.myRock = this.m_gameLogic.getRocker();
		}
		return this.myRock;
	}


	/**获取状态 */
	public getMyStates() {
		return this.myStates;
	}
	/**更改陀螺的状态 */
	//正常状态 摇杆可以接受信息，可以接受碰撞，
	//被碰撞状态，摇杆锁定，移动位置锁定，更改移动速度
	//放技能状态，摇杆锁定，接受碰撞，更改移动速度，
	//死亡状态，摇杆锁定，不接受碰撞

	public changeMyState(states, crashObj = null, shootdir = null, ) {


		if (states == this.myStates.lastState)
			return;


		//记录一下玩家的状态，防止被删除掉
		let mystate = this.myStates.state;

		//清除之前的所有状态，开始进行新的状态的绑定
		this.resetTopStates();




		//只有玩家的陀螺，才会有父场景选项
		if (this.m_gameLogic != null && this.isAI == false && states != TopState.normal) {
			//进行锁杆
			this.getGameRock().setLock(true)
		}
		let Kp = 1;
		if (states == TopState.normal) {
			this.dataTargetDir.setTo(0, 0);

		}
		else if (states == TopState.beCrash) {
			let blood = 0;

			//当双发都是射击状态，则进行等级对比
			if (mystate == TopState.shoot && crashObj.myStates.state === TopState.shoot) {
				blood = this.getSpeedLevel() - crashObj.getSpeedLevel();
				if (blood < 0)
					blood = 0;
				else
					blood = blood * 10;
			}
			else if (mystate == TopState.shoot) {
				blood = (serverConfig.config.speedRange.max + 1) - crashObj.getSpeedLevel();
				if (blood < 0)
					blood = 0;
				else
					blood = blood * 10;
			}
			else if (crashObj.myStates.state == TopState.shoot) {
				blood = (serverConfig.config.speedRange.max + 1) - this.getSpeedLevel();
				if (blood < 0)
					blood = 0;
				else
					blood = blood * 10;
			}
			else {
				blood = this.rotateSpeed - (this.rotateSpeed + crashObj.rotateSpeed) / 2;
				if (blood <= 0) {

				}
				blood = Math.abs(blood) * 8;

			}

			// let speed = this.rotateSpeed - crashObj.rotateSpeed;
			// //用来安装临时的反弹系数

			// if (speed == 0) {
			// 	Kp = 1;
			// }
			// else if (speed > 0) {
			// 	Kp = 0.2;
			// }
			// else {
			// 	Kp = speed / 10 + 1;
			// }

			//更改玩家的血量
			//	this.blood -= 10;
			console.log("myblood=" + blood);
			this.changeBlood(-1 * blood);
			this.changeTopGrade();
			//	console.log("Kp=" + Kp);

			//非空
			if (this.dataTargetDir == null) {
				console.log("basicTop 	this.dataTargetDir 是null")
				return null;
			}

			this.dataTargetDir.setTo(0, 0);
			let dir = this.calulatePopDir(this.dataPoint, crashObj.getDataPoint(this));

			//非空
			if (dir == null) {
				console.log("basicTop dir 为null");
				return null;
			}
			//	console.log("change user dir" + dir.x + "..." + dir.y);
			//下一帧的目标点
			this.dataTargetDir.setTo(dir.x, dir.y);


		}

		else if (states == TopState.shoot) {
			this.dataTargetDir.setTo(0, 0);
			this.dataTargetDir.setTo(shootdir.x, shootdir.y);
		}

		else if (states == TopState.bePk) {
			this.dataTargetDir.setTo(0, 0);
			let gamescene = this.m_gameLogic.getGameScene();

			//只有碰到了玩家才會彈出
			if (!this.isAI)
				gamescene.sendPKOrder(ShootControlState.spellHandSpeed);


			this.changeExpression(ExpressionState.spellHandSpeed);
			//	console.log("更换状态" + states + "名字" + this.nickName);
		}

		else if (states == TopState.death) {
			let gamescene = this.m_gameLogic.getGameScene();
			console.log(this.nickName + "更改為死亡狀態");
			//

		}
		else {
			console.log("未找到指定类型");
		}

		this.setStates(states, Kp);


	}
	/**更改表情 */
	public changeExpression(state: ExpressionState) {
		this.frogAnimationInit();
		this.top_Inner.visible = false;
		let image: egret.Texture;
		switch (state) {
			case ExpressionState.normal:
				image = resManager.getInstance().addRes("top_head1_1_png");
				this.top_Inner.texture = image;
				this.top_Inner.visible = true;
				break;
			case ExpressionState.abnormal:
				this.armatureDisplay = meterAanimation.getInstance().GetDragonAnimation(ExpressionState.abnormal);
				this.armatureDisplay.x = this.top_Inner.x;
				this.armatureDisplay.y = this.top_Inner.y;
				this.addChild(this.armatureDisplay);
				break;
			case ExpressionState.insane:
				this.armatureDisplay = meterAanimation.getInstance().GetDragonAnimation(ExpressionState.insane);
				this.armatureDisplay.x = this.top_Inner.x;
				this.armatureDisplay.y = this.top_Inner.y;
				this.addChild(this.armatureDisplay);
				break;
			case ExpressionState.spellHandSpeed:
				image = resManager.getInstance().addRes("top_head1_4_png");
				this.top_Inner.texture = image;
				this.top_Inner.visible = true;
				break;
			default:
				break;

		}
	}
	public changePkValue(num) {
		if (this.myStates.state == TopState.death) {
			console.log("该玩家已经死亡")
			return;
		}
		this.pkValue += num;
		this.showBloodText.text = this.pkValue + "";
	}
	public changeBlood(num) {

		if (this.myStates.state == TopState.death) {
			console.log("该玩家已经死亡")
			return;
		}


		this.blood += num;

		if (this.blood <= 0) {
			this.blood = 0;
			this.death();
		}
		this.changeBloodShow();
	}
	public isDeath() {
		return this.myStates.state == TopState.death;
	}
	public getPkValue() {
		return this.pkValue;
	}
	/**用于PK结束，更改状态，同时关闭点击界面 */
	public PKOverCallback(isDeath) {
		this.pkValue
	}
	/**设置状态 */
	public setStates(states, kp = 1) {
		if (states == TopState.normal)
			return;

		this.speed = this.changeSpeed(states, kp);
		this.myStates.lastState = states;
		this.myStates.state = states;
		this.myStates.statesStartTime = Date.now();
	}

	//发生了碰撞
	//1.陀螺，根据两个物体的转速，进行减速计算，更改碰撞状态，计算碰撞后的朝向，
	//2、道具则自我计算
	public beCrash(crashObj) {
		if (this.blood > 0) {
			//	console.log("发生碰撞喽~~掉血了");


			this.changeMyState(TopState.beCrash, crashObj);
			console.log("碰撞" + crashObj.nickName + "blood=" + this.blood);
			if (!this.isAI) {
				this.m_gameLogic.getGameScene().changeEnegry(30);
			}

			//	this.states = TopState.beCrash;
		}
	}

	//判断
	public bePk(crashObj) {
		if (this.myStates.state == TopState.death) {
			console.log("不要鞭尸");
			return;
		}


		this.setPKObj(crashObj);
		this.changeMyState(TopState.bePk, crashObj);
		//console.log("开始PK" + crashObj.nickName + "blood=" + this.blood);




	}
	/**更改陀螺当前所在等级区域 */
	public changeTopGrade() {
		//	this.showBloodText.text = this.blood + "";
		/**陀螺在哪个等级 */
		let grade = 0;
		if (this.blood >= 80) {
			grade = 0;
		} else if (this.blood >= 60) {
			grade = 1;
		} else if (this.blood >= 40) {
			grade = 2;
		} else if (this.blood >= 20) {
			grade = 3;
		} else if (this.blood > 0) {
			grade = 4;
		} else {
			console.log("陀螺死亡");

			return;
		}

		if (this.nowBloodSize != grade) {
			console.log("改變尺寸" + grade);
			this.changeTopSize(grade);
			this.nowBloodSize = grade;
		}

	}

	/**改变陀螺大小 */
	public changeTopSize(grade) {
		// //改变陀螺大小
		var tw = TweenManger.getInstance().getTweenObject(this, false);
		tw.to({ scaleX: this.radiusGears[grade] / serverConfig.config.TopInitSize, scaleY: this.radiusGears[grade] / serverConfig.config.TopInitSize }, 100);

		this.radius = this.radiusGears[grade];
		this.myCrashRange = this.radius * this.ratio;
		//改变名字的位置
		this.nameText.y += 5;
	}

	/**设置碰撞 */
	public setCrash(point) {

	}


	/**清空陀螺的状态 */
	public resetTopStates() {
		this.myStates.state = TopState.normal;
		this.myStates.statesStartTime = -1;
		this.myStates.lastState = TopState.normal;
		this.speed = serverConfig.config.speedSize.minSpeed;
		this.getGameRock().setLock(false);
		if (!this.isAI) {
			let gamescene = this.m_gameLogic.getGameScene();
			gamescene.sendPKOrder(ShootControlState.normal);
		}


	}
	/*获取更改状态的时间*/

	public getStatesTime(state) {
		let steatsTmpTime = 0
		switch (state) {
			case TopState.normal:
				steatsTmpTime = 0
				break;

			case TopState.death:
				steatsTmpTime = 1000
				break;
			case TopState.shoot:
				steatsTmpTime = 600
				break;

			case TopState.beCrash:
				steatsTmpTime = 100
				break;
			case TopState.bePk:
				steatsTmpTime = 200000
				break;

			default:
				console.log("getStatesTime 出現問題");
				break;
		}
		return steatsTmpTime;
	}

	/*用来检验点是否在范围之外*/
	protected checkBound(point, crashobj = null) {
		return this.m_gameLogic.getGameScene().checkCraskBound(point, this.myCrashRange, crashobj);
	}


	/**更改本人的狀態 */
	public updateMyState() {




	}

	/**添加动画 */
	public addAnimator(num: number) {
		let meterAni = meterAanimation.getInstance();
		this.armatureDisplay = meterAni.GetDragonAnimation(num);
		this.addChild(this.armatureDisplay);
		meterAni.openClock();
		this.armatureDisplay.visible = false;
	}
	/**动画初始化 */
	protected frogAnimationInit() {
		if (this.armatureDisplay) {
			this.removeChild(this.armatureDisplay);
			this.armatureDisplay.armature.dispose();
			this.armatureDisplay = null;
		}
	}
	/**显示血量临时使用 */
	public showBlood() {
		this.showBloodText = new eui.Label();
		this.showBloodText.text = this.blood + "";
		this.showBloodText.anchorOffsetX = this.showBloodText.width / 2;
		this.showBloodText.anchorOffsetY = this.showBloodText.height / 2;
		this.showBloodText.x = 0;
		this.showBloodText.y = 70;
		this.addChild(this.showBloodText);

	}
	public changeBloodShow() {
		this.showBloodText.text = this.blood + "";
	}

	/**显示更新 */
	public viewUpdate() {

		this.judgeIsPlayAnimation();
	}

	/**判断是否播放动画 */
	public judgeIsPlayAnimation() {
		return;
		if (this.rotateSpeed / serverConfig.config.maxSpeed >= 0.5) {
			this.armatureDisplay.visible = true;
		} else {
			this.armatureDisplay.visible = false;
		}
	}
}
/**表情状态 */
enum ExpressionState {
	normal, abnormal, insane, spellHandSpeed
}